package com.cssw.fyzb.web.service;

import com.alibaba.fastjson.JSON;
import com.cssw.fyzb.base.web.context.UserContext;
import com.cssw.fyzb.base.web.domain.AjaxData;
import com.cssw.fyzb.base.web.domain.AjaxPage;
import com.cssw.fyzb.base.web.domain.AjaxResult;
import com.cssw.fyzb.core.dao.system.OperDao;
import com.cssw.fyzb.core.dao.system.OperRoleDao;
import com.cssw.fyzb.core.model.dodb.SysOper;
import com.cssw.fyzb.core.model.dodb.SysOperRole;
import com.cssw.fyzb.core.util.BizUtil;
import com.cssw.fyzb.web.util.HashUtil;
import com.cssw.fyzb.web.util.StrUtil;
import com.ydn.dbframe.kit.StrKit;
import com.ydn.dbframe.plugin.activerecord.Db;
import com.ydn.dbframe.plugin.activerecord.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author Feng Chen
 */
@Service
public class OperService {

    @Autowired
    private OperDao operDao;

    @Autowired
    private OperRoleDao operRoleDao;

    private static final Logger logger = LoggerFactory.getLogger(OperService.class);


    /**
     * 查询账号列表
     *
     * @param page
     * @param limit
     * @param param
     * @return
     */
    public AjaxPage list(Integer page, Integer limit, Map param) {
        Page<SysOper> pages = operDao.list(param, page, limit);
        return AjaxPage.success(pages.getList(), pages.getTotalRow(), pages.getTotalPage());
    }

    public AjaxData info(Long operId) {
        return AjaxData.success(operDao.getById(operId));
    }

    /**
     * 新增账号信息
     *
     * @param operCode
     * @param operPwd
     * @param mobileNo
     * @param operName
     * @param status
     * @return
     */
    public AjaxData save(String operCode, String operPwd, String mobileNo,
                         String operName, String status, String remark, Long sysOperId) {
        SysOper oper = operDao.getByCode(operCode);
        if (null != oper) {
            BizUtil.exception("账号已经存在");
        }
        Db.tx(() -> {
            String hashOperPwd = HashUtil.md5(operPwd);
            operDao.save(operCode, hashOperPwd, mobileNo, operName, status, remark, sysOperId);
            return true;
        });

        return AjaxData.success();
    }

    /**
     * 修改该账号信息
     *
     * @param operId
     * @param operCode
     * @param mobileNo
     * @param operName
     * @param status
     * @return
     */
    public AjaxData update(Long operId, String operCode, String operPwd, String mobileNo,
                           String operName, String status, String remark, Long sysOperId) {
        SysOper oper = operDao.getByCode(operCode);
        if (null != oper
                && !StrUtil.equals(oper.getOperId().toString(), operId.toString())) {
            BizUtil.exception("账号已经存在");
        }

        Db.tx(() -> {
            operDao.update(operId, operCode, mobileNo, operName, status, remark, sysOperId);
            //如果密码不为空，修改密码
            if (StrKit.notBlank(operPwd)) {
                operDao.updateOperPwd(operId, HashUtil.md5(operPwd));
            }

            return true;
        });

        return AjaxData.success();
    }

    /**
     * 删除账号信息
     *
     * @param operIds
     * @return
     */
    public AjaxData delete(List<Long> operIds, Long sysOperId) {

        Db.tx(() -> {
            for (Long operId : operIds) {
                SysOper oper = operDao.getById(operId);

                operDao.delete(operId, sysOperId);
                operRoleDao.deleteByOperId(operId);
            }
            return true;
        });
        return AjaxData.success();
    }


    public AjaxData delete(Long operId, Long sysOperId) {
        operDao.delete(operId, sysOperId);
        return AjaxData.success();
    }

    /**
     * 停用账号
     *
     * @param operIds
     * @return
     */
    public AjaxData stop(List<Long> operIds, Long sysOperId) {
        Db.tx(() -> {
            for (Long operId : operIds) {
                operDao.updateStatus(operId, SysOper.STATUS_LOCK, sysOperId);
            }
            return true;
        });

        return AjaxData.success();
    }

    /**
     * 启用账号
     *
     * @param operIds
     * @return
     */
    public AjaxData start(List<Long> operIds, Long sysOperId) {
        Db.tx(() -> {
            for (Long operId : operIds) {
                operDao.updateStatus(operId, SysOper.STATUS_NORMAL, sysOperId);
            }
            return true;
        });

        return AjaxData.success();
    }

    public AjaxData queryOperRole(Long operId) {
        List<SysOperRole> operRoles = operDao.queryOperRole(operId);
        return AjaxData.success(operRoles);
    }


    public AjaxResult saveOperRole(Long operId, String roleList) {
        Db.tx(() -> {
            operDao.deleteOperRoleByOperId(operId);

            List<SysOperRole> operRoles = JSON.parseArray(roleList, SysOperRole.class);
            if (null != operRoles && operRoles.size() > 0) {
                for (SysOperRole operRole : operRoles) {
                    operRole.setOperId(operId);
                    operRole.setCreateOperId(UserContext.getUserId());
                    operRole.setCreateTime(new Date());
                }
                operDao.batchSaveOperRole(operRoles);
            }
            return true;
        });
        return AjaxResult.success();
    }

}
